package com.iteaj.iot.handle.proxy;

import com.iteaj.iot.ProtocolHandle;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public abstract class ProtocolHandleInvocationHandler implements InvocationHandler {

    private Object target;

    public ProtocolHandleInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(method.getName().equals(ProtocolHandle.method.getName())) {
            final Object invoke = method.invoke(target, args);
            if(invoke != null) {
                return proxyHandle(invoke, proxy);
            }

            return null;
        }

        return method.invoke(target, args);
    }

    protected abstract Object proxyHandle(Object value, Object proxy);

    public Object getTarget() {
        return target;
    }
}
